/**
 * @program: LeetCode
 * @description: LeetCode : 707. 设计链表
 * @author: WXY
 * @create: 2023-01-13 13:23
 * @Version 1.0
 **/
public class Num707_MyLinkList {
    class LinkNode{
        int val;
        LinkNode next;
        public LinkNode() {
        }
        public LinkNode(int val) {
            this.val = val;
        }
    }
    class MyLinkedList {
        LinkNode head ;
        int size;
        public MyLinkedList() {
            head = new LinkNode(0);
            size = 0;
        }

        public int get(int index) {
            if (index < 0 || index >=size) {
                return -1;
            }
            LinkNode cur = head;
            for (int i = 0; i <= index; i++) {
                cur = cur.next;
            }
            return cur.val;
        }

        public void addAtHead(int val) {
            addAtIndex(0, val);
        }

        public void addAtTail(int val) {
            addAtIndex(size, val);

        }

        public void addAtIndex(int index, int val) {
            if (index > size)return;
            if (index < 0)index = 0;
            LinkNode cur = head;
            size++;
            for (int i = 0; i < index; i++) {
                cur = cur.next;
            }
            LinkNode temp = new LinkNode(val);
            temp.next = cur.next;
            cur.next = temp;
        }

        public void deleteAtIndex(int index) {
            if (index >= size || index < 0)return;
            size--;
            if (index == 0) {
                head = head.next;
                return;
            }
            LinkNode cur = head;
            for (int i = 0; i < index; i++) {
                cur = cur.next;
            }
            cur.next = cur.next.next;
        }
    }
}
